package team.first.moiveapiv1.controller;

import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import team.first.moiveapiv1.domain.Genre;
import team.first.moiveapiv1.service.GenreService;
import team.first.moiveapiv1.vo.ApiResponse;

import java.util.List;

/**
 * @Author: HAPPY
 * @Project_name: project
 * @Package_name: team.first.moiveapiv1.controller
 * @Date: 2021/11/26 15:25
 * @Description:
 */
@RestController
public class GenreController {
    @Autowired
    GenreService genreService;

    /**
     * 1.1 获取全部电影类型信息
     * 接口地址：GET /api/v1/genres
     */
    @ApiOperation("1.1 GET: 获取全部电影类型信息")
    @GetMapping("/api/v1/genres")
    public ApiResponse<List<Genre>> genres() {
        ApiResponse<List<Genre>> apiResponse = new ApiResponse<>();
        try {
            apiResponse.setData(genreService.list()).setCode(200).setSuccess(true).setMessage("请求成功");
        } catch (Exception e) {
            apiResponse.setCode(500).setSuccess(false).setMessage("请求失败").setData(null);
        }
        return apiResponse;
    }

    /**
     * 2.7 电影分类信息增加（选做）
     * @param genre 添加的电影分类信息
     * @return 返回添加后的数据
     */
    @ApiOperation("2.7 POST 电影分类信息增加（选做）")
    @PostMapping("/api/v1/add/genre")
    public ApiResponse<Genre> addMovie(Genre genre) {
        ApiResponse<Genre> apiResponse = new ApiResponse<>();
        try{
            genreService.save(genre);
            // 前端根据返回值的状态
            apiResponse.setCode(200).setSuccess(true).setMessage("请求成功");
        } catch (Exception e) {
            apiResponse.setCode(500).setSuccess(false).setMessage("请求失败");
        }
        return apiResponse;
    }
}
